/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetchapp.ability;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.agp.components.ScrollView;
import ohos.agp.utils.Color;

import com.chinasoft_ohos.commontools.Toast.Toast;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2fileserver.FetchFileServer;
import com.tonyodev.fetch2rx.RxFetch;
import com.tonyodev.fetchapp.Data;
import com.tonyodev.fetchapp.ResourceTable;
import com.tonyodev.fetchapp.Utils;

import java.io.File;

/**
 * @since 2021-05-20
 */
public class MainAbility extends Ability {
    private static final int STORAGE_PERMISSION_CODE = 50;
    private Component mainView;
    private Intent intent;
    private Operation operation;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        getWindow().setStatusBarColor(Color.getIntColor("#303f9f"));
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        intent = new Intent();
        ScrollView scrollView = (ScrollView) findComponentById(ResourceTable.Id_scroll);
        scrollView.enableScrollBar(Component.DRAG_HORIZONTAL_VERTICAL, true);
        findComponentById(ResourceTable.Id_singleDemoButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_downloadListButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_gameFilesButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_multiEnqueueButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_multiFragmentButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_fileServerButton).setClickedListener(clickedListener);
        findComponentById(ResourceTable.Id_deleteAllButton).setClickedListener(clickedListener);
    }

    private Component.ClickedListener clickedListener = new Component.ClickedListener() {
        @Override
        public void onClick(Component component) {
            switch (component.getId()) {
                case ResourceTable.Id_singleDemoButton:
                    startAbility("com.tonyodev.fetchapp.ability.SingleDownloadAbility");
                    break;
                case ResourceTable.Id_downloadListButton:
                    startAbility("com.tonyodev.fetchapp.ability.DownloadListAbility");
                    break;
                case ResourceTable.Id_gameFilesButton:
                    startAbility("com.tonyodev.fetchapp.ability.GameFilesAbility");
                    break;
                case ResourceTable.Id_multiEnqueueButton:
                    startAbility("com.tonyodev.fetchapp.ability.FailedMultiEnqueueAbility");
//                    setTransitionAnimation(0,0);
                    break;
                case ResourceTable.Id_multiFragmentButton:
                    startAbility("com.tonyodev.fetchapp.ability.FragmentAbility");
                    break;
                case ResourceTable.Id_fileServerButton:
                    startAbility("com.tonyodev.fetchapp.ability.FileServerAbility");
                    break;
                case ResourceTable.Id_deleteAllButton:
                    deleteDownloadedFiles();
                    break;
                default:
                    break;
            }
        }
    };

    private void startAbility(String abilityName) {
        operation = new Intent.OperationBuilder()
            .withDeviceId("")
            .withBundleName(getBundleName())
            .withAbilityName(abilityName)
            .build();
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void deleteDownloadedFiles() {
        final String[] namespaces = new String[]{
            DownloadListAbility.FETCH_NAMESPACE,
            FailedMultiEnqueueAbility.FETCH_NAMESPACE,
            FileServerAbility.FETCH_NAMESPACE};
        for (String namespace : namespaces) {
            final FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(this).setNamespace(namespace).build();
            Fetch.Impl.getInstance(fetchConfiguration).deleteAll().close();
        }
        Fetch.Impl.getDefaultInstance().deleteAll().close();
        final RxFetch rxFetch = RxFetch.Impl.getDefaultRxInstance();
        rxFetch.deleteAll();
        rxFetch.close();
        try {
            new FetchFileServer.Builder(this)
                .setFileServerDatabaseName(FileServerAbility.FETCH_NAMESPACE)
                .setClearDatabaseOnShutdown(true)
                .build()
                .shutDown(false);
            final File fetchDir = new File(Data.getSaveDir(this));
            Utils.deleteFileAndContents(fetchDir);
            Toast.show(getString(ResourceTable.String_downloaded_files_deleted));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
